package com.mall.pay.wxpay.util;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class CommonUtil {
	public static boolean IsNumeric(String str) {
		if (str.matches("\\d *")) {
			return true;
		} else {
			return false;
		}
	}

	public static boolean IsAllNumeric(String str) {
		if (str.matches("^[0-9]*$")) {
			return true;
		} else {
			return false;
		}
	}

	public static String ArrayToXml(Map<String, String> arr) {
		String xml = "<xml>";

		Iterator<Entry<String, String>> iter = arr.entrySet().iterator();
		while (iter.hasNext()) {
			Entry<String, String> entry = iter.next();
			String key = entry.getKey();
			String val = entry.getValue();
			if (IsAllNumeric(val)) {
				xml += "<" + key + ">" + val + "</" + key + ">";

			} else
				xml += "<" + key + "><![CDATA[" + val + "]]></" + key + ">";
		}

		xml += "</xml>";
		return xml;
	}

	public static HashMap<String, String> parseXml(String strxml) throws Exception {
		// 解析结果存储在HashMap
		HashMap<String, String> map = new HashMap<String, String>();
		InputStream inputStream = new ByteArrayInputStream(strxml.getBytes("UTF-8"));
		// 读取输入流
		SAXReader reader = new SAXReader();
		Document document;
		try {
			document = reader.read(inputStream);
		} catch (Exception e) {
			e.printStackTrace();
			return map;
		}
		// 得到xml根元素
		Element root = document.getRootElement();
		// 得到根元素的所有子节点
		List<Element> elementList = root.elements();
		// 遍历所有子节点
		for (Element e : elementList) {
			map.put(e.getName(), e.getText());
		}
		// 释放资源
		inputStream.close();
		return map;
	}

	/**
	 * 
	 * @Title: getSendMessage
	 * @Description: 接收发送进来的xml解析
	 * @param request
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public static String getSendMessage(HttpServletRequest request) {
		BufferedReader br;
		try {
			StringBuilder sb = new StringBuilder();
			InputStream in = request.getInputStream(); // 支持中文
			int ch;
			while ((ch = in.read()) >= 0)
				sb.append((char) ch);
			String result = new String(sb.toString().getBytes("ISO-8859-1"), "utf-8");
			sb = null;
			return result;
		} catch (Exception e) {
			// log.error(e.getMessage(), e);
			return "";
		}
	}
	public static String getTimeStamp() {
		return String.valueOf(System.currentTimeMillis() / 1000);
	}

}
